【今日湯底】
Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.
It should remove all values from list a, which are present in list b keeping their order.
(= (array-diff [1 2] [1]) [2])
If a value is present in b, all of its occurrences must be removed from the other:
(= (array-diff [1,2,2,2,3] [2]) [1,3])
(必須通過以下測試)
(ns array-diff-test
(:require [clojure.test :refer :all]
[array-diff :refer :all]))
(deftest example-tests
(is (= (array-diff [1 2] [1]) [2]))
(is (= (array-diff [1 2 2] [1]) [2 2]))
(is (= (array-diff [1 2 2] [2]) [1]))
(is (= (array-diff [1 2 2] []) [1 2 2]))
(is (= (array-diff [1 2 3] [1 2]) [3]))
(is (= (array-diff [] [1 2]) [])))
【我的答案】
(ns array-diff)
(defn array-diff [a b]
(remove (set b) (seq a))
)
【其他人的答案】
(ns array-diff)
(defn array-diff [a b]
(remove (set b) a))
(ns array-diff)
(defn array-diff [a b]
(filter #(= -1 (.indexOf b %) ) a)
)